<script setup lang="ts">
import { uploadImage } from '@/apis/consult';
import type { Image } from '@/types/consult';
import { showLoadingToast, showToast, type UploaderAfterRead } from 'vant';
import { ref } from 'vue';

defineProps<{
  disabled: boolean
}>()
//定义响应式数据，接收用户发来的数据
const message = ref('')
const emit = defineEmits<{
  'send-msg': [msg: string],
  'send-img': [img: Image]
}>()
const onSendMsg = () => {
  //判断用户是否输入了内容
  if (message.value.trim() === '') return showToast('请输入内容')
  //将收集的数据发给父组件
  emit('send-msg', message.value)
  //清空聊天框
  message.value = ''
}
const onSendMImg: UploaderAfterRead = async (item) => {
  if (Array.isArray(item)) return
  if (!item.file) return
  const upload = showLoadingToast({ message: '正在上传', duration: 0 })
  const res = await uploadImage(item.file)
  upload.close()
  emit('send-img', res.data)
}
</script>

<template>
  <div class="room-action">
    <van-field type="text" v-model="message" class="input" :border="false" placeholder="问医生" autocomplete="off"
      :disabled="disabled" @keydown.enter="onSendMsg"></van-field>
    <van-uploader :after-read="onSendMImg" :preview-image="false" :disabled="disabled">
      <cp-icon name="consult-img" />
    </van-uploader>
  </div>
</template>

<style lang="scss" scoped>
.room-action {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1;
  box-sizing: border-box;

  .input {
    background-color: var(--cp-bg);
    border: none;
    border-radius: 25px;
    margin-right: 10px;
    padding: 8px 15px;
  }

  .cp-icon {
    width: 24px;
    height: 24px;
  }
}
</style>